<?php

namespace App\Controller;

use DCarbone\PHPConsulAPI\Catalog\CatalogRegistration;
use DCarbone\PHPConsulAPI\KV\KVPair;
use DCarbone\PHPConsulAPI\QueryOptions;
use DCarbone\PHPConsulAPI\WriteOptions;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use GuzzleHttp\Client as GuzzleClient;
use XiaoZhu\ServiceProxy\AgentProxy;
use DCarbone\PHPConsulAPI\Agent\AgentServiceRegistration;
use XiaoZhu\ServiceProxy\CatalogProxy;
use XiaoZhu\ServiceProxy\Config;
use XiaoZhu\ServiceProxy\KVProxy;

Config::setClient(new GuzzleClient([
    'timeout'  => 2.0,
]));
error_reporting(E_ALL);
//Config::setConsulAddress(["172.20.20.10:8500", '172.20.20.11:8500']);
class ExampleController extends AbstractController
{
    /**
     * @Route("/", name="example")
     * @param Request $request
     *
     * @return JsonResponse
     */
    public function index(Request $request)
    {
        dd($request);
    }

    /**
     * @Route("/catalog/services", name="catalog-services")
     * @param Request $request
     *
     */
    public function servicesList(Request $request)
    {
        $catalogProxy = CatalogProxy::getInstance();
        dd($catalogProxy->services()[0]);
    }

    /**
     * @Route("/service/remove", name="service-deregister1")
     * @param Request $request
     */
    public function serviceRemove123(Request $request)
    {
        $catalogProxy = CatalogProxy::getInstance();
        $agent = AgentProxy::getInstance();
        list($serviceInfo, $_, $err) = $catalogProxy->service($request->query->get("name"));
        if (is_array($serviceInfo)) {
            foreach ($serviceInfo as $k => $service) {
                dd($agent->serviceDeregister($service->ServiceID));
            }
        }

    }

    /**
     * @Route("/catalog/service", name="catalog-service")
     * @param Request $request
     *
     */
    public function serviceDetail(Request $request)
    {
        $catalogProxy = CatalogProxy::getInstance();
        dd($catalogProxy->service($request->get('name')));
    }

    /**
     * @Route("/agent/service/register", name="agent-service-register")
     * @param Request $request
     */
    public function serviceRegister(Request $request)
    {
        $service = new AgentServiceRegistration(array(
            'ID' => '/order/list',
            'Name' => 'order/list',
            'Address' => 'service-order.xiaozhu.com',
            'Port' => 80,
            'Meta' => array(
                'interface' => '/order/list',
            ))
        );

        $agent = AgentProxy::getInstance();
        $result = $agent->serviceRegister($service);
        dd($result);
    }

    /**
     * @Route("/agent/service/casServiceRegister", name="agent-service-casServiceRegister")
     * @param Request $request
     */
    public function casServiceRegister(Request $request)
    {
        $interfaces = array(
            '/order/index',
            '/order/list',
            '/order/detail',
        );
        $service = array(
            'order',
            '172.20.20.31',
            '8000',
            $interfaces
        );
        dd(AgentProxy::serviceRegister($service));
    }

    /**
     * @Route("/kv/set", name="kv-set")
     * @param Request $request
     *
     */
    public function kvSet(Request $request)
    {
        $kvProxy = KVProxy::getInstance();
        $pair = new KVPair(array(
            'Key' => $request->query->get('name'),
            'Value' => $request->query->get('value'),
        ));

        dd($kvProxy->cas($pair));
    }

    /**
     * @Route("/kv/get", name="kv-get")
     * @param Request $request
     *
     */
    public function kvGet(Request $request)
    {
        $kvProxy = KVProxy::getInstance();
        dd($kvProxy->get($request->query->get('name')));
    }


    function gen_uuid() {
        return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            // 32 bits for "time_low"
            mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

            // 16 bits for "time_mid"
            mt_rand( 0, 0xffff ),

            // 16 bits for "time_hi_and_version",
            // four most significant bits holds version number 4
            mt_rand( 0, 0x0fff ) | 0x4000,

            // 16 bits, 8 bits for "clk_seq_hi_res",
            // 8 bits for "clk_seq_low",
            // two most significant bits holds zero and one for variant DCE1.1
            mt_rand( 0, 0x3fff ) | 0x8000,

            // 48 bits for "node"
            mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
        );
    }

}
